<?php
  require_once('../db.php');

  if(isset($_GET['mode']) &&
  $_GET['mode'] == 'change_status' ) {
    $cStatus = $_GET['current_status'];
    $nStatus = $cStatus==0 ? 1:0;
    $id = $_GET['id'];
    $updateStatusQuery = "UPDATE `users`
    SET `status`='$nStatus'
    WHERE `id`='$id'";

    mysql_query($updateStatusQuery);
    if(mysql_affected_rows()==1){
      echo "Status Updated successfully.";
    }
  }

  if(isset($_GET['mode']) &&
  $_GET['mode'] == 'del' ) {
    $id = $_GET['id'];
    $delQuery = "DELETE FROM `users`
    WHERE `id`='$id'";
    print_q($delQuery);
    mysql_query($delQuery) or die(mysql_error());
    if(mysql_affected_rows() == 1){
      echo "<h4>Record Deleted successfully.</h4>";
    }
  }

  $totalRecordQuery = "SELECT COUNT(*) as `total` FROM `users`";
  $totalRecordResult = mysql_query($totalRecordQuery);
  $totalRecordAns = mysql_fetch_assoc($totalRecordResult);
  $totalUsers = $totalRecordAns['total'];
  $rpp = 2; # Offset
  $totalPages = ceil($totalUsers / $rpp);

  $c_page = isset($_GET['page']) && !empty($_GET['page']) ? $_GET['page'] : 1;

  $limit = ($c_page - 1) * $rpp;

  pr($totalRecordAns);

  $query = "SELECT * FROM `users` LIMIT $limit,$rpp";
  print_q($query);

  $result = mysql_query($query) or die(mysql_error());
?>
<p>
  <em>Total Records </em>:<strong><?=$totalUsers?></strong>
</p>

<table  border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
  <thead>
    <tr>
      <th colspan="6">:: ALL USERS ::</th>
    </tr>
    <tr>
      <th align="left">#</th>
      <th align="left">User Name</th>
      <th align="left">Email</th>
      <th align="left">Added date</th>
      <th align="left">Status</th>
      <th align="left">Action</th>
    </tr>
  </thead>
  <tbody>
<?php
  while($ans = mysql_fetch_assoc($result))
  {
    $id = $ans['id'];
?>
    <tr>
      <td ><?=$id?></td>
      <td ><?=$ans['uname']?></td>
      <td >

<a href="mailto:<?=$ans['email']?>"><?=$ans['email']?></a>
      </td>
      <td ><?=date('d-M-Y',strtotime($ans['added_date']))?></td>
      <td ><a href="users.php?mode=change_status&current_status=<?=$ans['status']?>&id=<?=$id?>"><?=getStatus($ans['status'])?></a></td>
      <td align="right">Edit / <a href="users.php?mode=del&id=<?=$id?>" onClick="return confirm('Are you sure ?')">Delete</a></td>
    </tr>
<?php
  }
?>
  </tbody>
</table>

<table  border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
    <tr>
<?php
for ($i = 1; $i <= $totalPages ; $i++) {
  if($c_page == $i){
?>
      <th><span style="color:red;font-size:25px;"><?=$i?></span></th>
<?php
  } else {
?>
 <th><a href="users.php?page=<?=$i?>"><?=$i?></a></th>
<?php
  }
}// end for
?>
    </tr>
</table>
